/*
 * Copyright 2021 the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * https://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openrewrite.groovy.tree

import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test

class ForLoopTest : GroovyTreeTest {
    @Test
    fun forLoopMultipleInit() = assertParsePrintAndProcess(
        // IntelliJ's Groovy support is confused by multiple assignment in a for loop,
        // but Groovy itself does support this construct:
        //    https://groovy-lang.org/semantics.html#_enhanced_classic_java_style_for_loop
        """
            int i
            int j
            for(i = 0, j = 0;;) {
            }
        """
    )

    @Test
    fun forLoopMultipleUpdate() = assertParsePrintAndProcess(
        """
           int i = 0
           int j = 10
           for(; i < j; i++ , j-- ) { }
        """
    )

    @Test
    fun forLoop() = assertParsePrintAndProcess(
        """
            for(int i = 0; i < 10; i++) {
            }
        """
    )

    @Test
    fun infiniteLoop() = assertParsePrintAndProcess(
        """
            for(;;) {
            }
        """
    )

    @Test
    fun format() = assertParsePrintAndProcess(
        """
            for ( int i = 0 ; i < 10 ; i++ ) {
            }
        """
    )

    @Test
    fun formatInfiniteLoop() = assertParsePrintAndProcess(
        """
            for ( ; ; ) {}
        """
    )

    @Test
    fun formatLoopNoInit() = assertParsePrintAndProcess(
        """
            for ( ; i < 10 ; i++ ) {}
        """
    )

    @Test
    fun formatLoopNoCondition() = assertParsePrintAndProcess(
        """
            int i = 0;
            for(; i < 10; i++) {}
        """
    )

    @Test
    fun statementTerminatorForSingleLineForLoops() = assertParsePrintAndProcess(
        """
            for(;;) test()
        """
    )

    @Test
    fun initializerIsAnAssignment() = assertParsePrintAndProcess(
        """
            def a = [1,2]
            int i=0
            for(i=0; i<a.length; i++) {}
        """
    )

    @Disabled
    @Test
    fun multiVariableInitialization() = assertParsePrintAndProcess(
        """
            for(int i, j = 0;;) {}
        """
    )

    @Test
    fun forEachWithColon() = assertParsePrintAndProcess(
        """
            for(int i : [1, 2, 3]) {}
        """
    )

    @Test
    fun forEachWithIn() = assertParsePrintAndProcess(
        """
            for(int i in [1, 2, 3]) {}
        """
    )
}
